package org.micode.demo.web.controller;

import org.micode.common.annotation.SysLog;
import org.micode.common.enums.OperationModeEnum;
import org.micode.common.helper.I18nMsgHelper;
import org.micode.common.validater.UpdateGroup;
import org.micode.demo.web.base.BaseWebAdminController;
import org.micode.common.annotation.PermissionsFlag;
import org.micode.common.domain.ApiResult;
import org.micode.common.enums.ResultEnum;
import org.micode.common.exception.DataException;
import org.micode.demo.biz.vo.MenuVO;
import org.micode.demo.db.domain.dto.SysMenuDTO;
import org.micode.demo.web.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.micode.demo.web.vo.DisableUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单信息的APP控制类
 * <p>
 * author: wutianbin
 */
@RestController
@RequestMapping("/web-admin/menu")
@Slf4j
public class MenuController
        extends BaseWebAdminController {

    @Autowired
    private MenuService menuService;

    /**
     * 导航菜单
     */
    @PostMapping("/navigation")
    public ApiResult<MenuVO<SysMenuDTO>> navigation() {
        log.debug(I18nMsgHelper.get("api.call.menu.navigation"));
        Long userId = currentUserId();
        if (null == userId) {
            throw new DataException(I18nMsgHelper.get("valid.user.not-login"));
        }

        return invokeMethod(menuService::getUserNavigationMenus, userId,
                ResultEnum.VERIFY_ERROR, I18nMsgHelper.get("api.except.menu.navigation"));
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @PostMapping("/select")
    @PermissionsFlag("sys:menu:select")
    public ApiResult<List<SysMenuDTO>> select() {
        log.debug(I18nMsgHelper.get("api.call.menu.select"));
        //查询列表数据
        List<SysMenuDTO> menuList = menuService.queryNotButtonList();
        return successResultWithData(menuList);
    }

    /**
     * 禁用处理，禁用/启用都是这个接口处理
     *
     * @param disableUpdateVO 禁用信息
     * @return 处理影响的记录数
     */
    @SysLog(value = "{api.syslog.menu.disable}", operationMode = OperationModeEnum.UPDATE)
    @PostMapping("/disable")
    @PermissionsFlag("sys:menu:update")
    public ApiResult<Integer> disable(
            @RequestBody @Validated(UpdateGroup.class) DisableUpdateVO<Long> disableUpdateVO) {
        log.debug(I18nMsgHelper.get("api.call.menu.disable") + " == {}", disableUpdateVO);
        SysMenuDTO updateDTO = new SysMenuDTO();
        updateDTO.setMenuId(disableUpdateVO.getId());
        updateDTO.setDisableFlag(disableUpdateVO.getDisableFlag());
        return updateTheDTO(menuService::update, updateDTO);
    }

}
